package com.ggh.jinjilive.ui.mall.shop.adapter;

import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.bumptech.glide.Glide;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemMallGoodsBinding;
import com.ggh.jinjilive.databinding.ItemSearchBarBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.listener.AddOnClickListener;
import com.ggh.jinjilive.mobile.mall.MallGoods;

import java.util.List;

public class ShopAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<MallGoods> mallGoodsList;

    public void setAdd(List<MallGoods> mallGoodsList) {
        this.mallGoodsList = mallGoodsList;
        notifyDataSetChanged();
    }

    public void ADD(List<MallGoods> mallGoodsList) {
        if(null !=  this.mallGoodsList){
            this.mallGoodsList.addAll(mallGoodsList);
        }else {
            this.mallGoodsList = mallGoodsList;
        }
        notifyDataSetChanged();
    }


    private AddItemOnClickListener addItemOnClickListener;
    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }

    private AddOnClickListener addOnClickListener;
    public void setAddItemOnClickListener(AddOnClickListener addOnClickListener) {
        this.addOnClickListener = addOnClickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemMallGoodsBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_mall_goods, parent, false);
        return new ItemViewHolder(binding);
    }

    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder recyclerViewholder,final int position) {
       if(recyclerViewholder instanceof ItemViewHolder){
            ItemViewHolder holder = (ItemViewHolder) recyclerViewholder;
            MallGoods mallGoods = mallGoodsList.get(position);
            if (null != mallGoods) {
                //图片
                Glide.with(holder.binding.RoundedImageView).load(mallGoods.getImg()).into(holder.binding.RoundedImageView);
                //名称
                holder.binding.tvName.setText(mallGoods.getName());
                //价格
                holder.binding.tvPrice.setText("¥" + mallGoods.getPrice());
                //已购
                holder.binding.tvSales.setText(mallGoods.getSales() + "人已购");
                //内容
                holder.binding.tvDescribes.setText(mallGoods.getDescribes());
                //
                holder.binding.llGoods.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(null != addItemOnClickListener){
                            addItemOnClickListener.onSkipClicked(position,mallGoods);
                        }
                    }
                });
            }
        }

    }

    @Override
    public int getItemCount() {
        return null == mallGoodsList ? 0 : mallGoodsList.size();
    }


    class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemMallGoodsBinding binding;
        public ItemViewHolder(@NonNull ItemMallGoodsBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
            ViewGroup.LayoutParams params =  binding.llGoods.getLayoutParams();
            params.height = ScreenUtils.getScreenHeight() / 3 - SizeUtils.dp2px(20);
            binding.llGoods.setLayoutParams(params);
        }
    }

}